package com.workflow.workflowproject.listener;

import com.workflow.workflowproject.entity.FlowLogs;
import com.workflow.workflowproject.mapper.FlowLogsMapper;
import com.workflow.workflowproject.until.LoginUserUtil;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngines;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.impl.FlowableProcessCancelledEventImpl;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 流程删除（流程驳回等操作）
 */
@Slf4j
@Component
public class DeleteFlowListener implements FlowableEventListener {
    @Autowired
    private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    @Autowired
    private FlowLogsMapper flowLogsMapper;
    private static DeleteFlowListener deleteFlowListener;

    @PostConstruct
    public void init(){
        deleteFlowListener = this;
        deleteFlowListener.processEngine = this.processEngine;
        deleteFlowListener.flowLogsMapper =this.flowLogsMapper;
    }

    @Override
    public void onEvent(FlowableEvent event) {
        log.info(" -------------------------------------------流程删除时监听-------------------------start",event);
        FlowableProcessCancelledEventImpl engineEvent = (FlowableProcessCancelledEventImpl)event;
        ProcessInstance processInstance = processEngine.getRuntimeService().createProcessInstanceQuery()
                .processInstanceId(engineEvent.getScopeId())
                .singleResult();
        log.info("流程删除时监听");
        log.info("进入类型:" + event.getType());
        log.info("流程实例ID:" + engineEvent.getScopeId());
        log.info("流程业务ID：" + processInstance.getBusinessKey());
        TaskService taskService = processEngine.getTaskService();
        Task task = taskService.createTaskQuery().processInstanceId(engineEvent.getScopeId()).active().singleResult();
        log.info("任务节点:" + task.getTaskDefinitionKey());
        log.info("任务名称：" + task.getName());
        //查询流程任务
        FlowLogs flowLogs = new FlowLogs();
        flowLogs.setId(task.getId());
        flowLogs.setType(event.getType().toString());
        flowLogs.setBusKey(processInstance.getBusinessKey());
        flowLogs.setProcessInstanceId(engineEvent.getScopeId());
        flowLogs.setNodeKey(task.getTaskDefinitionKey());
        flowLogs.setTaskName(task.getName());
        flowLogs.setResult("流程驳回");
        flowLogs.setUserId("user");
        deleteFlowListener.flowLogsMapper.insert(flowLogs);
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return "";
    }
}
